package com.source.jucutils;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author wengqy
 * @Date 2021/1/20
 */
public class CountDownLatchDemo {

    private  static  CountDownLatch countDownLatch =  new CountDownLatch(40);

    public static void main(String[] args) {

        Emp emp = new Emp(countDownLatch);
        Boss boss  = new Boss(countDownLatch);
        new Thread(boss,"BOSS").start();




        for (int i = 1; i <= 40; i++) {

            new Thread(emp,"EMP: "+String.valueOf(i)).start();

        }



     }
}





class Boss implements   Runnable{


    private CountDownLatch countDownLatch ;

    public Boss(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }
    @Override
    public void run() {
        System.out.println("共有 "+countDownLatch.getCount()+"  个人在开会");

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("开始  开会");

    }
}


class Emp implements   Runnable{
    private CountDownLatch countDownLatch ;

    public Emp(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
         try{
           //  TimeUnit.MILLISECONDS.sleep(200);
               Thread.sleep(200);
               System.out.println("线程： "+Thread.currentThread().getName()+"  进来了");

         }catch(Exception e){

         }finally {
             countDownLatch.countDown();
         }


    }
}
